package com.blacktea.behavioralpatterns.interpreterpattern;

/**
 * @description: 大于
 * 具体的表达式类
 * @author: black tea
 * @date: 2023/3/25 15:23
 */
public class GreaterThanExpression implements Expression {
    private final String variableName;
    private final int value;

    public GreaterThanExpression(String variableName, int value) {
        this.variableName = variableName;
        this.value = value;
    }

    @Override
    public boolean interpret(Context context) {
        Object variableValue = context.getVariableValue(variableName);
        if (variableValue == null) {
            return false;
        }
        try {
            int intValue = (int) variableValue;
            return intValue > value;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}